﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using loginSQL.SQLHelper;

namespace loginSQL.Util
{
    public class License
    {
        public string id;
        public string license_id;
        public string license_level_id;
        public string license_name;
        public string license_level_name;
        public string tanggal_pengeluaran;
        public string berlaku_hingga;

        //perpanjangan print count
        public int current_perpanjangan_print_index;
        //klasifikasi rating print count
        public int current_klasifikasi_rating_print_index;
        //perpanjangan rating print count
        public int current_perpanjangan_rating_print_index;
        //catatan print count
        public int current_catatan_print_index;

        public List<Renewals> renewals = new List<Renewals>();
        public List<RatingClassification> ratingclassification = new List<RatingClassification>();
        public List<RatingRenewals> ratingrenewals = new List<RatingRenewals>();
        public List<string> comments = new List<string>();

        public DBHelper dbHelper;
        
                                 
        /*public License(string id, string license_id, string license_level_id)
        {
            this.id = id;
            this.license_id = license_id;
            this.license_level_id = license_level_id;
            this.dbHelper = new DBHelper();

            fillDataLicenseName();
            fillDataTanggal();
            fillDataRenewals();
            fillDataRatingClassification();
            fillDataRatingRenewals();
            fillComments();


        }*/

        public License(string id, string license_id, string license_level_id, 
            int current_perpanjangan_print_index,
            int current_klasifikasi_rating_print_index,
            int current_perpanjangan_rating_print_index,
            int current_catatan_print_index)
        {
            // TODO: Complete member initialization
            this.id = id;
            this.license_id = license_id;
            this.license_level_id = license_level_id;
            this.current_perpanjangan_print_index = current_perpanjangan_print_index;
            this.current_klasifikasi_rating_print_index = current_klasifikasi_rating_print_index;
            this.current_perpanjangan_rating_print_index = current_perpanjangan_rating_print_index;
            this.current_catatan_print_index = current_catatan_print_index;

            this.dbHelper = new DBHelper();

            fillDataLicenseName();
            fillDataTanggal();
            fillDataRenewals();
            fillDataRatingClassification();
            fillDataRatingRenewals();
            fillComments();
        }

        private void fillDataLicenseName()
        {
            this.license_name = dbHelper.getLicenseName(this.license_id);
            this.license_level_name = dbHelper.getLicenseLevelName(this.license_level_id);
        }

        private void fillDataTanggal()
        {
            List<string> tanggal = dbHelper.getLicenseDate(this.id, this.license_id, this.license_level_id);
            if (tanggal.Count > 0)
            {
                this.tanggal_pengeluaran = tanggal[0];
                this.berlaku_hingga = tanggal[1];
            }
            else
            {
                this.tanggal_pengeluaran = "-";
                this.berlaku_hingga = "-";
            }

        }

        private void fillDataRenewals()
        {
            List<List<string>> perpanjanganLisensi = dbHelper.getPerpanjanganLisensi(this.license_id, this.license_level_id);
            foreach (List<string> list in perpanjanganLisensi)
            {
                if (list.Count > 0)
                {
                    Renewals r = new Renewals(list[0], list[1]);
                    this.renewals.Add(r);
                }                
            }
        }

        private void fillDataRatingClassification()
        {
            List<List<string>> klasifikasi_rating = dbHelper.getKlasifikasiRating(this.id, this.license_id, this.license_level_id);
            foreach (List<string> list in klasifikasi_rating)
            {
                if (list.Count > 0)
                {
                    RatingClassification rc = new RatingClassification(list[0], list[1]);
                    this.ratingclassification.Add(rc);
                }
            }
        }

        private void fillDataRatingRenewals()
        {
            List<List<string>> perpanjangan_rating = dbHelper.getPerpanjanganRating(this.id, this.license_id, this.license_level_id);
            foreach(List<string> list in perpanjangan_rating)
            {
                if (list.Count > 0)
                {
                    RatingRenewals rr = new RatingRenewals(list[0], list[1]);
                    this.ratingrenewals.Add(rr);
                }
            }
        }

        private void fillComments()
        {
            List<string> comments = dbHelper.getCatatan(this.id, this.license_id, this.license_level_id);
            foreach (string s in comments)
            {
                this.comments.Add(s);
            }
        }
    }
}
